//
//  GLShowPickerView.m
//  KTools
//
//  Created by 121 on 2017/9/25.
//  Copyright © 2017年 杭州吉利控股信息中心. All rights reserved.
//

#define GLShowPickerView_ContentHegiht 250.0
#define GLShowPickerView_Header 40
#import "GLPickerView.h"
#import "Masonry.h"



@interface GLPickerView()<UIGestureRecognizerDelegate,UIPickerViewDataSource, UIPickerViewDelegate>
@property(nonatomic,strong)UIView *contentView;
@property(nonatomic,strong)UIView *header;
@property(nonatomic,strong)UIView *headerLine;
@property(nonatomic,strong)UIButton *cancleBtn;
@property(nonatomic,strong)UIButton *sureBtn;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UIPickerView *pickerView;

@property(nonatomic,assign)NSInteger component;

@property(nonatomic,copy)NSArray* weights;
@property(nonatomic,assign)NSInteger weightSum;

@property(nonatomic,copy)NSArray* datas;
@property(nonatomic,copy)NSArray* subKeys;
//event
@property(nonatomic,copy)GLPickerCompetionBlock competion;
@property(nonatomic,copy)GLPickerStringCovertBlock convert;
//mutilp
@property(nonatomic,copy)GLPickerMutableCompetionBlock mcompetion;
@property(nonatomic,copy)GLPickerStringMutableCovertBlock mconvert;
@property(nonatomic,assign)BOOL columnLinkage;

//custom picker block
@property(nonatomic,copy)GLPickerNumOfComponentBlock cblockNumOfComponent;
@property(nonatomic,copy)GLPickerNumOfRowsBlock cblockNumOfRows;
@property(nonatomic,copy)GLPickerTitleViewBlock cblockTitleView;
@property(nonatomic,copy)GLPickerDidSelectRowBlock cblockDidSelected;
@property(nonatomic,copy)GLPickerCompetionBlock cblockCompetion;


@end
 
@implementation GLPickerView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupDefaultData];
        
    }
    return self;
}

- (void)setupDefaultData{
    self.columnLinkage = YES;
}

- (void)setupUI
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackGround:)];
    [self addGestureRecognizer:tap];
  
    [self setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.f]];
    
    [self addSubview:self.contentView];
    
    [self.contentView addSubview:self.header];
    [self.contentView addSubview:self.pickerView];
    
    [self.header addSubview:self.titleLabel];
    [self.header addSubview:self.sureBtn];
    [self.header addSubview:self.cancleBtn];
    [self.header addSubview:self.headerLine];
    
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self);
        make.height.mas_equalTo(GLShowPickerView_ContentHegiht);
        make.centerX.equalTo(self);
        make.top.equalTo(self.mas_bottom);
    }];
    
    [self.header mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.contentView);
        make.height.mas_equalTo(GLShowPickerView_Header);
        make.left.top.equalTo(self.contentView);
    }];
    
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.contentView);
        make.top.equalTo(self.header.mas_bottom);
        make.bottom.left.equalTo(self.contentView);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.header);
    }];
    
    [self.cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.top.equalTo(self.header);
        make.left.equalTo(self.header).offset(15);
        make.centerY.equalTo(self.header);
    }];
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.top.equalTo(self.header);
        make.right.equalTo(self.header).offset(-15);
        make.centerY.equalTo(self.header);
    }];
    
    [self.headerLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.header);
        make.height.mas_equalTo(1.0/[UIScreen mainScreen].scale);
    }];
}
#pragma mark- API
+ (instancetype)showDatas:(NSArray*)datas convert:(GLPickerStringCovertBlock)convert competion:(GLPickerCompetionBlock)competion{
    GLPickerView* picker = [[GLPickerView alloc]init];
    picker.datas = datas;
    picker.component = 1;
    picker.convert = convert;
    picker.competion = competion;
    [picker show];
    return picker;
}

+ (instancetype)showDatas:(NSArray*)datas component:(NSInteger)component subArrayKey:(NSArray*)keys  convert:(GLPickerStringMutableCovertBlock)convert competion:(GLPickerMutableCompetionBlock)competion{
    return [self showDatas:datas component:component weights:nil subArrayKey:keys convert:convert competion:competion];
}

+ (instancetype)showDatas:(NSArray*)datas component:(NSInteger)component weights:(NSArray*)weights subArrayKey:(NSArray*)keys  convert:(GLPickerStringMutableCovertBlock)convert competion:(GLPickerMutableCompetionBlock)competion{
    GLPickerView* picker = [[GLPickerView alloc]init];
    picker.datas = datas;
    picker.component = component;
    picker.mconvert = convert;
    picker.mcompetion = competion;
    picker.subKeys = keys;
    picker.weights = weights;
    [picker show];
    return picker;
}

//nolinkage
+ (instancetype)showNoLinkageDatas:(NSArray*)datas component:(NSInteger)component weights:(NSArray*)weights convert:(GLPickerStringMutableCovertBlock)convert competion:(GLPickerMutableCompetionBlock)competion{
    GLPickerView* picker = [[GLPickerView alloc]init];
    picker.datas = datas;
    picker.component = component;
    picker.mconvert = convert;
    picker.mcompetion = competion;
    picker.weights = weights;
    picker.columnLinkage = NO;
    [picker show];
    return picker;
}

+ (instancetype)showCustomePickerByWeights:(NSArray*)weights numOfComponent:(GLPickerNumOfComponentBlock)numOfComponent numOfRows:(GLPickerNumOfRowsBlock)numOfRows titleView:(GLPickerTitleViewBlock)configView didSelected:(GLPickerDidSelectRowBlock)selectedBlock competion:(GLPickerCompetionBlock)competion{
    GLPickerView* picker = [[GLPickerView alloc]init];
    picker.weights = weights;
    picker.cblockNumOfComponent = numOfComponent;
    picker.cblockNumOfRows = numOfRows;
    picker.cblockTitleView = configView;
    picker.cblockDidSelected = selectedBlock;
    picker.cblockCompetion = competion;
    [picker show];
    return picker;
}

- (void)show{
    
    UIWindow* keyWindow = [[UIApplication sharedApplication]keyWindow];
    [keyWindow addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.equalTo(keyWindow);
    }];
    
    [self setupUI];
    
    [self layoutIfNeeded];
    [UIView animateWithDuration:0.3f animations:^{
        [self setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:.2f]];
        [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_bottom).offset(-GLShowPickerView_ContentHegiht);
        }];
        [self layoutIfNeeded];
    } completion:^(BOOL finished){
        
    }];
}

- (void)dismiss{

    [UIView animateWithDuration:0.3f animations:^{
        [self setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.f]];
        [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_bottom);
        }];
        [self layoutIfNeeded];
    } completion:^(BOOL finished){
        [self removeFromSuperview];
    }];
}
#pragma mark- event
-(void)cancelButtonClick:(UIButton*)btn{
    if (self.cblockCompetion) {
        self.cblockCompetion(GLPickerEventCancel, nil);
    }
    else if (self.mcompetion) {
        self.mcompetion(GLPickerEventCancel, nil);
    }
    else if (self.competion) {
        self.competion(GLPickerEventCancel, nil);
    }
    [self dismiss];
    
}
-(void)sureButtonClick:(UIButton*)btn{
    if (self.cblockCompetion) {
        self.cblockCompetion(GLPickerEventSure, nil);
    }
    else if (self.mcompetion) {
        NSMutableArray* values = [NSMutableArray arrayWithCapacity:0];
        
        for (NSInteger index = 0;index < self.component;index++) {
            NSInteger row = [self.pickerView selectedRowInComponent:index];
            NSArray* currDatas = nil;
            if(!self.columnLinkage){
                currDatas = [self.datas objectAtIndex:index];
            }else{
                currDatas = [self currDatasByComponent:index];
            }
            
            id value = [currDatas objectAtIndex:row];
            [values addObject:value];
        }
        self.mcompetion(GLPickerEventSure, values);
    }
    else if (self.competion) {
        id value = nil;
        
        if (_pickerView && self.datas.count > 0) {
            NSInteger row = [self.pickerView selectedRowInComponent:0];
            value = [self.datas objectAtIndex:row];
        }

        
        self.competion(GLPickerEventSure, value);
    }
    [self dismiss];
    
}
-(void)tapBackGround:(UITapGestureRecognizer*)tap{
    //点击内容header不作取消处理
    CGPoint tapP = [tap locationInView:self.contentView];
    if (tapP.x > 0 && tapP.y > 0) {
        return;
    }
    [self cancelButtonClick:nil];
}
#pragma mark- other
-(NSArray*)currDatasByComponent:(NSInteger)component{
    NSArray* subArray = self.datas;
    for (NSInteger index = 0;index <component;index++) {
        NSInteger selectRow = [self.pickerView selectedRowInComponent:index];
        
        NSString* subKey = [self.subKeys objectAtIndex:index];
        if (selectRow >= subArray.count) {
            selectRow = subArray.count-1;
        }
        id object = [subArray objectAtIndex:selectRow];
        subArray = [object valueForKey:subKey];
    }
    return subArray;
}

#pragma mark- UIPickerView DataSource And Delegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (self.cblockNumOfComponent) {
        return self.cblockNumOfComponent(pickerView);
    }
    return self.component;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (self.cblockNumOfRows) {
        return self.cblockNumOfRows(pickerView,component);
    }
    else if (self.columnLinkage) {
        NSArray* datas = [self currDatasByComponent:component];
        return datas.count;
    }
    else{
        NSArray* subArray = self.datas[component];
        return subArray.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString* strValue = nil;
    if (self.columnLinkage) {
        NSArray* datas = [self currDatasByComponent:component];
        if (row >= datas.count) {
            row = datas.count-1;
        }
        if (row >= 0) {
            strValue = [datas objectAtIndex:row];
        }
        
    }else{
        NSArray* subArray = self.datas[component];
        
        strValue = [subArray objectAtIndex:row];
    }
    
    if ([strValue isKindOfClass:[NSString class]]) {
        return strValue;
    }else{
        if (self.mconvert) {
            return self.mconvert(strValue,component);
        }
        else if(self.convert) {
            return self.convert(strValue);
        }
    }
    return @"error";
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    if ([[[UIDevice currentDevice]systemVersion] floatValue] > 10.0) {
        //设置分割线的颜色 (针对ios10系统添加)
        for(UIView *singleLine in pickerView.subviews)
        {
            if (singleLine.frame.size.height < 1)
            {
                singleLine.backgroundColor = [UIColor colorWithRed:222.0/255.0 green:222.0/255.0 blue:222.0/255.0 alpha:1];
            }
        }
    }
    
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.textColor = [UIColor blackColor];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        [pickerLabel setFont:[UIFont systemFontOfSize:14.0f]];
    }
    
    if (self.cblockTitleView) {
        return self.cblockTitleView(pickerView, row, component, pickerLabel);
    }else{
        pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
        return pickerLabel;
    }
    
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (self.weights) {
        NSInteger curWeight = [[self.weights objectAtIndex:component]integerValue];
        return (self.bounds.size.width-5*(self.component-1))*((CGFloat)curWeight/(CGFloat)self.weightSum);
    }else{
        return (self.bounds.size.width-5*(self.component-1))/self.component;
    }
    
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 30;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (self.cblockDidSelected) {
        self.cblockDidSelected(pickerView, row, component);
    }
    else if (self.columnLinkage) {
        for (NSInteger index = component+1;index < self.component;index++) {
            [pickerView reloadComponent:index];
            [pickerView selectRow:0 inComponent:index animated:YES];
        }
    }
}
#pragma mark- setter and getter
-(UIView*)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = [UIColor colorWithRed:238/255.0f green:238/255.0f blue:238/255.0f alpha:1.0f];
    }
    return _contentView;
}
-(UIView*)header{
    if (!_header) {
        _header = [[UIView alloc] init];
        _header.backgroundColor = [UIColor whiteColor];
        _header.userInteractionEnabled = YES;
    }
    return _header;
}

-(UIView*)headerLine{
    if (!_headerLine) {
        _headerLine = [[UIView alloc] init];
        _headerLine.backgroundColor = [UIColor lightGrayColor];
    }
    return _headerLine;
}

-(UIButton*)cancleBtn{
    if (!_cancleBtn) {
        _cancleBtn = [[UIButton alloc]init];
        _cancleBtn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
        [_cancleBtn setBackgroundColor:[UIColor clearColor]];
#ifdef GLS_COLOR_BLUE_3D9EFC
        [_cancleBtn setTitleColor:GLS_COLOR_BLUE_3D9EFC forState:UIControlStateNormal];
#else
        [_cancleBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
#endif
        [_cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancleBtn addTarget:self action:@selector(cancelButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancleBtn;
}

-(UIButton*)sureBtn{
    if (!_sureBtn) {
        _sureBtn = [[UIButton alloc]init];
        _sureBtn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
        [_sureBtn setBackgroundColor:[UIColor clearColor]];
#ifdef GLS_COLOR_BLUE_3D9EFC
        [_sureBtn setTitleColor:GLS_COLOR_BLUE_3D9EFC forState:UIControlStateNormal];
#else
        [_sureBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
#endif
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_sureBtn addTarget:self action:@selector(sureButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}

-(UILabel*)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = [UIFont systemFontOfSize:16.0];
        _titleLabel.textColor = [UIColor lightGrayColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text = @"请选择";
    }
    return _titleLabel;
}

-(UIPickerView*)pickerView{
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] init];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
    }
    return _pickerView;
}



-(void)setWeights:(NSArray *)weights{
    if (_weights != weights) {
        _weights = weights;
        _weightSum = 0;
        for (NSNumber* num in _weights) {
            _weightSum+=[num integerValue];
        }
    }
}


@end


@interface GLDatePickerView ()
@property(nonatomic,assign)GLDatePickerMode mode;
@property(nonatomic,assign)NSDate* selectedDate;
@property(nonatomic,strong)NSDateComponents* selectDateComponents;
@property(nonatomic,strong)NSDateComponents* minDateComponents;
@property(nonatomic,strong)NSDateComponents* maxtDateComponents;

@property(nonatomic,strong)NSArray* componentOfTime;
@property(nonatomic,copy)GLDatePickerCompetionBlock dateCompetionBlock;
@end
static const NSCalendarUnit unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday;

@implementation GLDatePickerView
-(instancetype)init{
    self = [super init];
    if (self) {
        NSDate* minDate = [GLDatePickerView setYear:1900 month:1 day:1 hour:0 minute:0 second:0];
        NSDate* maxDate = [GLDatePickerView setYear:2100 month:12 day:31 hour:24 minute:60 second:60];
        
        self.minDateComponents = [GLDatePickerView dateComponentsForDate:minDate];
        self.maxtDateComponents = [GLDatePickerView dateComponentsForDate:maxDate];
    }
    return self;
}

+ (NSDate *)setYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *currentDate = [NSDate date];
    NSDateComponents *components = [calendar components:unitFlags fromDate:currentDate];
    [components setYear:year];
    [components setMonth:month];
    [components setDay:day];
    NSDate *date = [calendar dateFromComponents:components];
    return date;
}

+ (NSDate *)setYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day hour:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *currentDate = [NSDate date];
    NSDateComponents *components = [calendar components:unitFlags fromDate:currentDate];
    [components setYear:year];
    [components setMonth:month];
    [components setDay:day];
    [components setHour:hour];
    [components setMinute:minute];
    [components setSecond:second];
    NSDate *date = [calendar dateFromComponents:components];
    return date;
}


+ (NSDateComponents *)dateComponentsForDate:(NSDate*)date{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:unitFlags fromDate:date];
    return components;
}

+ (instancetype)showDate:(GLDatePickerMode)mode minDate:(NSDate*)minDate maxDate:(NSDate*)maxDate selectdDate:(NSDate*)date competion:(GLDatePickerCompetionBlock)competion{
    GLDatePickerView* picker = [[GLDatePickerView alloc]init];
    picker.mode = mode;
    [picker configMinDate:minDate maxDate:maxDate];
    if(!date){
        date = [NSDate date];
    }
    picker.selectedDate = date;
    picker.dateCompetionBlock = competion;
    [picker show];
    [picker scrollToSelectRow];
    return picker;
}

+ (instancetype)showDate:(GLDatePickerMode)mode competion:(GLDatePickerCompetionBlock)competion{
    return [self showDate:mode minDate:nil maxDate:nil selectdDate:nil competion:competion];
}

- (void)configMinDate:(NSDate*)minDate maxDate:(NSDate*)maxDate{
    if (!minDate && !maxDate) {
        return;
    }
    if ([minDate compare:maxDate] != NSOrderedAscending) {
        return;
    }
    self.minDateComponents = [GLDatePickerView dateComponentsForDate:minDate];
    self.maxtDateComponents = [GLDatePickerView dateComponentsForDate:maxDate];
}

- (void)scrollToSelectRow{
    for (NSInteger i=0;i<self.componentOfTime.count;i++) {
        GLDatePickerTime time = [[self.componentOfTime objectAtIndex:i]integerValue];

        NSInteger rowValue = [self.selectDateComponents valueForComponent:time];
        NSInteger row = [self getRowByValue:rowValue time:time];

        [self.pickerView selectRow:row inComponent:i animated:NO];
    }
}

#pragma mark- event
#pragma mark- event
-(void)cancelButtonClick:(UIButton*)btn{
    if (self.dateCompetionBlock) {
        self.dateCompetionBlock(GLPickerEventCancel, nil);
    }
    [self dismiss];
    
}
-(void)sureButtonClick:(UIButton*)btn{
    
    if (self.dateCompetionBlock) {
        self.dateCompetionBlock(GLPickerEventSure, self.selectDateComponents);
    }
    [self dismiss];
    
}


#pragma mark- UIPickerView DataSource And Delegate

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self getNumberOfRowsInComponent:component];
}


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel) {
        pickerLabel = (UILabel*)[super pickerView:pickerView viewForRow:row forComponent:component reusingView:view];
    }
    pickerLabel.text = [self getTitleInComponent:component row:row];
    pickerLabel.font = [UIFont systemFontOfSize:16];
    return pickerLabel;
    
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    for (NSInteger index = component;index < self.componentOfTime.count;index++) {
        if (index != component) {
            [pickerView reloadComponent:index];
        }
        
        GLDatePickerTime subtime = [[self.componentOfTime objectAtIndex:index]integerValue];
        NSInteger subRow = [pickerView selectedRowInComponent:index];
        NSInteger subValue = [self getValueByRow:subRow time:subtime];
        [self.selectDateComponents setValue:subValue forComponent:subtime];
    }
    
    NSLog(@"selectTime:%@",self.selectDateComponents);
}

#pragma mark-

#pragma mark-
- (NSInteger)getValueByRow:(NSInteger)row time:(GLDatePickerTime)time{
    NSInteger value = row;
    switch (time) {
        case GLDatePickerTimeYear:
            value = self.minDateComponents.year+row;
            break;
        case GLDatePickerTimeMonth:
            if (self.minDateComponents.year == self.selectDateComponents.year) {
                value = row+self.minDateComponents.month;
            }else{
                value = 1+row;
            }
            break;
        case GLDatePickerTimeDay:
            if (self.minDateComponents.year == self.selectDateComponents.year&&
                self.minDateComponents.month == self.selectDateComponents.month) {
                value = row+self.minDateComponents.day;
            }else{
                value = 1+row;
            }
            break;
        case GLDatePickerTimeHour:
            if (self.minDateComponents.year == self.selectDateComponents.year&&
                self.minDateComponents.month == self.selectDateComponents.month&&
                self.minDateComponents.day == self.selectDateComponents.day) {
                value = row+self.minDateComponents.hour;
            }else{
                value = row;
            }
            break;
        case GLDatePickerTimeMinute:
            if (self.minDateComponents.year == self.selectDateComponents.year&&
                self.minDateComponents.month == self.selectDateComponents.month&&
                self.minDateComponents.day == self.selectDateComponents.day&&
                self.minDateComponents.hour == self.selectDateComponents.hour) {
                value = row+self.minDateComponents.minute;
            }else{
                value = row;
            }
            break;
        case GLDatePickerTimeSecond:
            if (self.minDateComponents.year == self.selectDateComponents.year&&
                self.minDateComponents.month == self.selectDateComponents.month&&
                self.minDateComponents.day == self.selectDateComponents.day&&
                self.minDateComponents.hour == self.selectDateComponents.hour&&
                self.minDateComponents.minute == self.selectDateComponents.minute) {
                value = row+self.minDateComponents.second;
            }else{
                value = row;
            }
            break;
        default:
            break;
    }
    return value;
}

- (NSInteger)getRowByValue:(NSInteger)value time:(GLDatePickerTime)time{
    NSInteger row = value;
    switch (time) {
        case GLDatePickerTimeYear:
            row = value - self.minDateComponents.year;
            break;
        case GLDatePickerTimeMonth:
            if (self.minDateComponents.year == self.selectDateComponents.year) {
                row = value-self.minDateComponents.month;
            }else{
                row = value-1;
            }
            break;
        case GLDatePickerTimeDay:
            if (self.minDateComponents.year == self.selectDateComponents.year&&
                self.minDateComponents.month == self.selectDateComponents.month) {
                row = value - self.minDateComponents.day;
            }else{
                row = value - 1;
            }
            break;
        case GLDatePickerTimeHour:
            if (self.minDateComponents.year == self.selectDateComponents.year&&
                self.minDateComponents.month == self.selectDateComponents.month&&
                self.minDateComponents.day == self.selectDateComponents.day) {
                row = value-self.minDateComponents.hour;
            }else{
                row = value;
            }
            break;
        case GLDatePickerTimeMinute:
            if (self.minDateComponents.year == self.selectDateComponents.year&&
                self.minDateComponents.month == self.selectDateComponents.month&&
                self.minDateComponents.day == self.selectDateComponents.day&&
                self.minDateComponents.hour == self.selectDateComponents.hour) {
                row = value-self.minDateComponents.minute;
            }else{
                row = value;
            }
            break;
        case GLDatePickerTimeSecond:
            if (self.minDateComponents.year == self.selectDateComponents.year&&
                self.minDateComponents.month == self.selectDateComponents.month&&
                self.minDateComponents.day == self.selectDateComponents.day&&
                self.minDateComponents.hour == self.selectDateComponents.hour&&
                self.minDateComponents.minute == self.selectDateComponents.minute) {
                row = value-self.minDateComponents.second;
            }else{
                row = value;
            }
            break;
        default:
            break;
    }
    return row;
}

- (NSInteger)numOfDaysInThisYear:(NSInteger)year withMonth:(NSInteger)month{
    if((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12))
        return 31 ;
    if((month == 4) || (month == 6) || (month == 9) || (month == 11))
        return 30;
    if((year % 4 == 1) || (year % 4 == 2) || (year % 4 == 3))
        return 28;
    if(year % 400 == 0)
        return 29;
    if(year % 100 == 0)
        return 28;
    return 29;
}

-(NSInteger)getNumberOfRowsByTime:(GLDatePickerTime)time{
    NSInteger numOfRows = 0;
    switch (time) {
            
        case GLDatePickerTimeYear:
        {
            numOfRows = self.maxtDateComponents.year - self.minDateComponents.year;
            numOfRows++;
            break;
        }
        case GLDatePickerTimeMonth:
        {
            if (self.maxtDateComponents.year == self.selectDateComponents.year) {
                numOfRows = self.maxtDateComponents.month;
            }else{
                numOfRows = 12;
            }
            
            if (self.minDateComponents.year == self.selectDateComponents.year) {
                numOfRows-=self.minDateComponents.month;
                numOfRows++;
            }
            
            
            break;
        }
        case GLDatePickerTimeDay:
        {
            NSInteger days = [self numOfDaysInThisYear:self.selectDateComponents.year withMonth:self.selectDateComponents.month];
            if (self.maxtDateComponents.year == self.selectDateComponents.year&&
                self.maxtDateComponents.month == self.selectDateComponents.month) {
                numOfRows = self.maxtDateComponents.day;
                if (numOfRows > days) {
                    numOfRows = days;
                }
            }else{
                numOfRows = days;
            }
            
            
            if (self.minDateComponents.year == self.selectDateComponents.year&&
                self.minDateComponents.month == self.selectDateComponents.month) {
                numOfRows-=self.minDateComponents.day;
                numOfRows++;
            }
            
            break;
        }
        case GLDatePickerTimeHour:
        {
           
            if (self.maxtDateComponents.year == self.selectDateComponents.year&&
                self.maxtDateComponents.month == self.selectDateComponents.month&&
                self.maxtDateComponents.day == self.selectDateComponents.day) {
                numOfRows = self.maxtDateComponents.hour;
                numOfRows++;
            }else{
                numOfRows = 24;
            }
            
            
            if (self.minDateComponents.year == self.selectDateComponents.year&&
                self.minDateComponents.month == self.selectDateComponents.month&&
                self.minDateComponents.day == self.selectDateComponents.day) {
                numOfRows-=self.minDateComponents.hour;
            }
            break;
        }
        case GLDatePickerTimeMinute:
        {
            if (self.maxtDateComponents.year == self.selectDateComponents.year&&
                self.maxtDateComponents.month == self.selectDateComponents.month&&
                self.maxtDateComponents.day == self.selectDateComponents.day&&
                self.maxtDateComponents.hour == self.selectDateComponents.hour) {
                numOfRows = self.maxtDateComponents.minute;
                numOfRows++;
                
            }else{
                numOfRows = 60;
            }
            
            if (self.minDateComponents.year == self.selectDateComponents.year&&
                self.minDateComponents.month == self.selectDateComponents.month&&
                self.minDateComponents.day == self.selectDateComponents.day&&
                self.minDateComponents.hour == self.selectDateComponents.hour) {
                numOfRows-=self.minDateComponents.minute;
            }
            break;
        }
        case GLDatePickerTimeSecond:
        {
            if (self.maxtDateComponents.year == self.selectDateComponents.year&&
                self.maxtDateComponents.month == self.selectDateComponents.month&&
                self.maxtDateComponents.day == self.selectDateComponents.day&&
                self.maxtDateComponents.hour == self.selectDateComponents.hour&&
                self.maxtDateComponents.minute == self.selectDateComponents.minute) {
                numOfRows = self.maxtDateComponents.second;
                numOfRows++;
                
            }else{
                numOfRows = 60;
            }
            
            if (self.minDateComponents.year == self.selectDateComponents.year&&
                self.minDateComponents.month == self.selectDateComponents.month&&
                self.minDateComponents.day == self.selectDateComponents.day&&
                self.minDateComponents.hour == self.selectDateComponents.hour&&
                self.minDateComponents.minute == self.selectDateComponents.minute) {
                numOfRows-=self.minDateComponents.second;
            }
            break;
        }
        default:
            break;
    }
    
    return numOfRows;
}

-(NSInteger)getNumberOfRowsInComponent:(NSInteger)component{
    GLDatePickerTime time = [[_componentOfTime objectAtIndex:component]integerValue];
    NSInteger numOfRows = [self getNumberOfRowsByTime:time];
    
    return numOfRows;
}

-(NSString*)getTitle:(GLDatePickerTime)time row:(NSInteger)row{
    NSString* rowTitle = nil;
    switch (time) {
            
        case GLDatePickerTimeYear:
        {
            NSInteger value = [self getValueByRow:row time:time];
            rowTitle = [NSString stringWithFormat:@"%04ld 年",value];
            break;
        }
        case GLDatePickerTimeMonth:
        {
            NSInteger value = [self getValueByRow:row time:time];
            rowTitle = [NSString stringWithFormat:@"%02ld 月",value];
            break;
        }
        case GLDatePickerTimeDay:
        {
            NSInteger value = [self getValueByRow:row time:time];
            rowTitle = [NSString stringWithFormat:@"%02ld 日",value];
            break;
        }
        case GLDatePickerTimeHour:
        {
            NSInteger value = [self getValueByRow:row time:time];
            rowTitle = [NSString stringWithFormat:@"%02ld 时",value];
            break;
        }
        case GLDatePickerTimeMinute:
        {
            NSInteger value = [self getValueByRow:row time:time];
            rowTitle = [NSString stringWithFormat:@"%02ld 分",value];
            break;
        }
        case GLDatePickerTimeSecond:
        {
            NSInteger value = [self getValueByRow:row time:time];
            rowTitle = [NSString stringWithFormat:@"%02ld 秒",value];
            break;
        }
        default:
            break;
    }
    if (!rowTitle) {
        rowTitle = @"error";
    }
    return rowTitle;
}

-(NSString*)getTitleInComponent:(NSInteger)component row:(NSInteger)row{
    GLDatePickerTime time = [[_componentOfTime objectAtIndex:component]integerValue];
    NSString* rowTitle = [self getTitle:time row:row];
    
    if (!rowTitle) {
        rowTitle = @"error";
    }
    return rowTitle;
}

#pragma mark- setter and getter

-(void)setMode:(GLDatePickerMode)mode{
    _mode = mode;

    switch (_mode) {
            
        case GLDatePickerModeYear://年
        {
            _componentOfTime = @[@(GLDatePickerTimeYear)];
            
            self.weights = @[@1];
            self.component = 1;
            break;
        }
        case GLDatePickerModeYearAndMonth://年 月
        {
            _componentOfTime = @[@(GLDatePickerTimeYear),@(GLDatePickerTimeMonth)];
            self.weights = @[@3,@2];
            self.component = 2;
            break;
        }
        case GLDatePickerModeDate://年 月 日
        {
            _componentOfTime = @[@(GLDatePickerTimeYear),@(GLDatePickerTimeMonth),@(GLDatePickerTimeDay)];
            self.weights = @[@3,@2,@2];
            self.component = 3;
            break;
        }
        case GLDatePickerModeDateHourMinute://年 月 日 时 分
        {
            _componentOfTime = @[@(GLDatePickerTimeYear),@(GLDatePickerTimeMonth),@(GLDatePickerTimeDay),@(GLDatePickerTimeHour),@(GLDatePickerTimeMinute)];
            self.weights = @[@2,@2,@2,@2,@2];
            self.component = 5;
            break;
        }
        case GLDatePickerModeDateHourMinuteSecond://年 月 日 时 分 秒
        {
            _componentOfTime = @[@(GLDatePickerTimeYear),@(GLDatePickerTimeMonth),@(GLDatePickerTimeDay),@(GLDatePickerTimeHour),@(GLDatePickerTimeMinute),@(GLDatePickerTimeSecond)];
            self.weights = @[@1,@1,@1,@1,@1,@1];
            self.component = 6;
            break;
        }
        case GLDatePickerModeTime://时 分
        {
            _componentOfTime = @[@(GLDatePickerTimeMinute),@(GLDatePickerTimeSecond)];
            self.weights  = @[@1,@1];
            self.component = 2;
            break;
        }
        case GLDatePickerModeTimeAndSecond://时分秒
        {
            _componentOfTime = @[@(GLDatePickerTimeHour),@(GLDatePickerTimeMinute),@(GLDatePickerTimeSecond)];
            self.weights = @[@1,@1,@1];
            self.component = 3;
            break;
        }
        default:
            break;
    }
}

-(void)setSelectedDate:(NSDate *)selectedDate{
    _selectedDate = selectedDate;
    _selectDateComponents = [GLDatePickerView dateComponentsForDate:_selectedDate];
    switch (_mode) {
            
        case GLDatePickerModeYear://年
        {
            _selectDateComponents.month = 0;
            _selectDateComponents.day = 0;
            _selectDateComponents.hour = 0;
            _selectDateComponents.minute = 0;
            _selectDateComponents.second = 0;
            break;
        }
        case GLDatePickerModeYearAndMonth://年 月
        {
            _selectDateComponents.day = 0;
            _selectDateComponents.hour = 0;
            _selectDateComponents.minute = 0;
            _selectDateComponents.second = 0;
            break;
        }
        case GLDatePickerModeDate://年 月 日
        {
            _selectDateComponents.hour = 0;
            _selectDateComponents.minute = 0;
            _selectDateComponents.second = 0;
            break;
        }
        case GLDatePickerModeDateHourMinute://年 月 日 时 分
        {
            _selectDateComponents.second = 0;
            break;
        }
        case GLDatePickerModeDateHourMinuteSecond://年 月 日 时 分 秒
        {
            break;
        }
        case GLDatePickerModeTime://时 分
        {
            _selectDateComponents.year = 0;
            _selectDateComponents.month = 0;
            _selectDateComponents.day = 0;
            _selectDateComponents.hour = 0;

            break;
        }
        case GLDatePickerModeTimeAndSecond://时分秒
        {
            _selectDateComponents.year = 0;
            _selectDateComponents.month = 0;
            _selectDateComponents.day = 0;
            break;
        }
        default:
            break;
    }
}

@end
